home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / adjram41.zip / ADJRAM.C next >
C/C++ Source or Header  |  1988-05-15  |  53KB  |  1,509 lines

  1. /*      Adjustable Ram Disk
  2.  
  3.  (c)    Copyright 1986,1987 by Gary Cramblitt.  All Rights Reserved.
  4.  
  5.  
  6.  v2.2    1 Jul 86   Initial version
  7.  v2.3   24 Aug 86   Bug.  FAT media byte not updated properly.
  8.  v2.4   29 Aug 86   If current drive is memory disk drive, reset current
  9.                     directory on exit.
  10.  v2.5   30 Aug 86   Increase FAT to permit max size of 2043K;
  11.                     Increase size of root directory to 128 entries;
  12.                     Start code for /E option (Expanded Memory Support)
  13.  v3.0   30 Aug 86   Finish code for /E option.
  14.  v3.1    2 Oct 86   Fix shrink bug.  Not packing subdirectories properly
  15.  v3.2   18 Oct 86   Allow SIZE= clause for AMDISK.  Also allow drive letters
  16.                     A through L.
  17.  v4.0    3 Jan 87   Add reserved conventional memory support as
  18.                       R:xxxx:nn option.
  19.                     Full support for expanded memory mixed with conventional
  20.                       memory.
  21.                     Max size of memory disk based upon available memory and
  22.                       cluster size.  
  23.                     User requested expansion or shrinkage automatically
  24.                       adjusted to within limits and rounded to nearest
  25.                       memory block boundary.
  26.                     In expanded memory, use larger block size (64K) to avoid
  27.                       excessive usage of EMM handles.
  28.                     Display Memory Block Table only if /t option specified.
  29.                     Bug.  Stop processing directory when first "never_used"
  30.                       flag is encountered.
  31.                     Bug in DeSmet free() routine.  Causes memory overflo.
  32.                       Calculate stack space manually instead.
  33.                     Restrict program size to under 32K.
  34.                     Clean up error reporting.  Tell user if error has
  35.                       corrupted the memory disk.
  36. v4.1    15 May 88   Restrict reserved memory to above segment a000.
  37.                     Initialize reserved memory when block is allocated.
  38.  
  39.         For program usage, see the last routine.
  40.  
  41.         A few definitions:
  42.                 1.  CONVENTIONAL MEMORY is that memory directly addressable
  43.                     by the 8088 or 8086 CPU and controlled by DOS.  On the
  44.                     IBM PC, it falls below 640K.
  45.                 2.  RESERVED MEMORY is also directly addressable by the
  46.                     8088 or 8086, but it is not controlled by DOS.  On
  47.                     the IBM PC, it falls above 640K and below 1MB.  It
  48.                     is usually reserved for use by the PC's BIOS.  If your
  49.                     machine has memory mapped to addresses between 640K and
  50.                     1MB and that memory is not needed for any other purpose,
  51.                     and it is directly addressable by the CPU (no special
  52.                     "paging" or "mapping" required to access it), then
  53.                     ADJRAM can use it via the R option.  See .DOC
  54.                 3.  EXTENDED MEMORY is addressed above 1MB.  It is only
  55.                     available on AT type machines (80286 or 80386 CPUs).
  56.                     It is not currently supported by ADJRAM.
  57.                 4.  EXPANDED MEMORY is memory conforming to the Lotus/
  58.                     Intel/Microsoft Expanded Memory Specification.  It
  59.                     is supported by ADJRAM if you have the EMM loaded.
  60.  
  61.         This program is coded in DeSmet C v2.4.  Any function beginning
  62. with underscore (_) is a non-standard routine from the DeSmet library.
  63. They are:
  64.         _showcs()
  65.                 Synopsis: unsigned _showcs()
  66.  
  67.                 Returns the current value of CS.
  68.  
  69.         _showds()
  70.                 Synopsis: unsigned _showds()
  71.  
  72.                 Returns the current value of DS (= SS in DeSmet C).
  73.  
  74.         _showsp()
  75.                 Synopsis: char *_showsp()
  76.  
  77.                 Returns the current value of SP.
  78.  
  79.         _setsp()
  80.                 Synopsis: void _setsp(newStackValue);
  81.                           unsigned newStackValue;
  82.  
  83.                 Sets the SP register to the specified value.
  84.  
  85.         _memory()
  86.                 Synopsis: char *_memory()
  87.  
  88.                 Returns a pointer to the first byte of free memory past
  89.                 the end of initialized memory (global data).
  90.  
  91.         _peek()
  92.                 Synopsis: char _peek(offset,segment)
  93.                           char *offset;
  94.                           unsigned segment;
  95.                 Returns the byte at specified far address.
  96.  
  97.         _doint()
  98.                 Synopsis: set any or all of the externs
  99.                           _rax,_rbx,_rcx,_rdx,_rsi,_rdi,_res,_rds
  100.                 followed by
  101.                           _doint(interrupt number);
  102.                 Performs the specified interrupt with the specified
  103.                 registers set from the externs.
  104.  
  105.                 After the call, _rax, etc. can be used.  _carryf
  106.                 and _zerof are extern char variables set to 1 if
  107.                 the carry or zero flag is set.
  108.  
  109. In addition, the following routines are semi_standard, and may have
  110. slightly different implementations with your compiler:
  111.  
  112.         strncmp()
  113.                 Synopsis: char *strncmp(leftstring,rightstring,max)
  114.                           char *leftstring, *rightstring
  115.                           int max               
  116.                 strncmp() compares up to a specified number of chars
  117.                 in two strings.  It returns 0 if the specified number
  118.                 of characters in the string are the same.
  119.  
  120. */
  121.  
  122. /* ==== Definitions ==== */
  123.  
  124. /* ---- Overall Definitions ---- */
  125.  
  126. #define true                    1
  127. #define false                   0
  128.  
  129. /*
  130.    ---- To compile without LOTUS/INTEL/Microsoft Expanded Memory
  131.         support, change the "1" to "0" in the next statement.  Will
  132.         save about 1500 bytes in EXE file.
  133. */
  134.  
  135. #define em_support              1       /* compile for EM support */
  136.  
  137. /* ---- To compile without reserved memory support, change
  138.         the "1" to "0" in the next statement.
  139. */
  140.  
  141. #define rm_support              1       /* compile for reserved memory */
  142.  
  143. #define debug                   0       /* compile with debug stmts */
  144.  
  145. /* ---- Other customizable symbols */
  146.  
  147. #define start_rm_addr        0xa000    /* start of reserved memory */
  148.  
  149. /*
  150.         The following symbols must correspond to the same symbols in
  151.         file amdisk.asm.  If one is changed, so must the other.
  152.         This is because the first memory block may not be
  153.         deallocated.
  154. */
  155.  
  156. #define def_size_K              64      /* default to 64K RAM disk */
  157. #define min_size_K              (boot_sec.mem_blk_table[0].siz/sec_per_K)
  158. #define min_size_sec            boot_sec.mem_blk_table[0].siz
  159. #define em_sec_per_blk          512     /* increment in 256K Expanded   */
  160.                                         /*   Memory blocks              */
  161. #define cnv_sec_per_blk         64      /* increment in 32K conventional*/
  162.                                         /*   memory blocks              */
  163.  
  164. /*
  165.    ---- Disk definitions.  Note: These constants should be made into
  166.         variables or functions if this program's algorithms need to
  167.         be generalized to disks of any type, especially high density disks.
  168.         Since this program works only in conjunction with amdisk.asm,
  169.         it is OK to make them constants here.
  170. */
  171.  
  172. #define bytes_per_sec           512     /* 512 bytes per sector */
  173. #define par_per_sec             (512/16)
  174.                                         /* 32 paragraphs per sector */
  175. #define sec_per_K               (1024/512)
  176.                                         /* sectors per 1024 bytes */
  177. #define dir_per_sec             (512/sizeof(struct dir_entry))
  178.                                         /* directory entries per sector */
  179. #define K_per_blk               (sec_per_blk/sec_per_K)
  180.                                         /* K per memory block */
  181. #define em_pag_per_blk          (K_per_blk/16)
  182.                                         /* 16K EM pages per mem block */
  183. #define max_clu